"""
ip提取
"""

import requests
from bs4 import BeautifulSoup
import redis
import json


def extract(url):
    return requests.get(url)


def loadData():
    r = extract("https://ip.51240.com/?dz=%E5%B9%BF%E4%B8%9C")
    html = BeautifulSoup(r.content, "lxml")
    target = html.find_all("tr")
    dic = {}
    for td in target:
        if td.text.find("广东省") > 0 and len(td.text) < 300:
            spilt = td.text.split("广")
            ip_range = spilt[0].strip().split("-")

            ip_tmp_list = []
            for ip in ip_range:
                ip_split = ip.split(".")
                if 0 < int(ip_split[2]) <= 255:
                    for i in range(int(ip_split[2]), 255):
                        if 0 < int(ip_split[3]) <= 255:
                            for j in range(int(ip_split[2]), 255):
                                ip_tmp_list.append(ip_split[0] + "." + ip_split[1] + "." + str(i) + "." + str(j))
            if len(ip_tmp_list) > 0:
                dic["广" + spilt[1]] = ip_tmp_list

    return dic


data = loadData()
con = redis.ConnectionPool(host="localhost", port=6379, db=2)
op = redis.Redis(connection_pool=con)
for (k, v) in data.items():
    op.hset(k.strip(), k.strip(), json.dumps(v))
